package com.cloudweb.oa.service.impl;

import cn.js.fan.util.DateUtil;
import com.cloudweb.oa.entity.OaMessageDept;
import com.cloudweb.oa.mapper.OaMessageDeptMapper;
import com.cloudweb.oa.service.IOaMessageDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fgf
 * @since 2024-05-31
 */
@Service
public class OaMessageDeptServiceImpl extends ServiceImpl<OaMessageDeptMapper, OaMessageDept> implements IOaMessageDeptService {

    @Autowired
    private OaMessageDeptMapper oaMessageDeptMapper;

    @Override
    public OaMessageDept getByDeptCode(String deptCode) {
        return oaMessageDeptMapper.getByDeptCode(deptCode);
    }

    @Override
    public boolean setAgent(String deptCode, String agentDeptCode, Date agentDate) {
        OaMessageDept oaMessageDept = oaMessageDeptMapper.getByDeptCode(deptCode);
        if (oaMessageDept == null) {
            oaMessageDept = new OaMessageDept();
            oaMessageDept.setDeptCode(deptCode);
            oaMessageDept.setAgentDeptCode(agentDeptCode);
            oaMessageDept.setAgentDate(DateUtil.toLocalDate(agentDate));
            oaMessageDept.setAgented(true);
            return oaMessageDept.insert();
        } else {
            oaMessageDept.setAgentDeptCode(agentDeptCode);
            oaMessageDept.setAgentDate(DateUtil.toLocalDate(agentDate));
            oaMessageDept.setAgented(true);
            return updateById(oaMessageDept);
        }
    }

    @Override
    public boolean cancelAgent(String deptCode) {
        OaMessageDept oaMessageDept = oaMessageDeptMapper.getByDeptCode(deptCode);
        if (oaMessageDept == null) {
            return false;
        }
        oaMessageDept.setAgentDeptCode("");
        oaMessageDept.setAgentDate(null);
        oaMessageDept.setAgented(false);
        return updateById(oaMessageDept);
    }
}
